package com.bw.goods.bind;

import android.view.LayoutInflater;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.databinding.BindingAdapter;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;
import androidx.viewpager.widget.ViewPager;

import com.alibaba.android.arouter.launcher.ARouter;
import com.bw.goods.R;
import com.bw.goods.adapter.HomeBannerAdapter;
import com.bw.goods.adapter.HomeMenuAdapter;
import com.bw.goods.adapter.HomeRecyclerAdapter;
import com.bw.goods.adapter.HomeTextBannerAdapter;
import com.bw.goods.adapter.HomeTopAdapter;
import com.bw.goods.mvvm.model.entity.HomeBannerEntity;
import com.bw.goods.mvvm.model.entity.HomeMenuEntity;
import com.bw.mvvm.arouter.MyARouter;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemChildClickListener;
import com.youth.banner.Banner;

import org.jetbrains.annotations.NotNull;

import java.util.List;

public class GoodsBind {
    @BindingAdapter(value = {"goodsadapter","imagbanner","txtbanner",
            "menudata","hotdata","topEntities","transformer"})
    public static void bindGoodsRc(RecyclerView recyclerView,
                                   BaseQuickAdapter quickAdapter,
                                   List<HomeBannerEntity> imagEntities,
                                   List<String> textbanner,
                                   List<HomeMenuEntity> menuEntities,
                                   List<String> hotdata,
                                   List<HomeBannerEntity> topEntities,
                                   ViewPager.PageTransformer pageTransformer){
        StaggeredGridLayoutManager sglm = new StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(sglm);
        View headView = LayoutInflater.from(recyclerView.getContext()).inflate(R.layout.view_home_banner,null);
        //创建顶部 banner轮播图
        Banner home_view_banner = headView.findViewById(R.id.home_view_banner);
        home_view_banner.setAdapter(new HomeBannerAdapter(imagEntities))
                .setOrientation(Banner.HORIZONTAL)
                .start();
        //创建公告 banner轮播
        Banner home_text_banner = headView.findViewById(R.id.home_text_banner);
        home_text_banner.setAdapter(new HomeTextBannerAdapter(textbanner))
                .setOrientation(Banner.VERTICAL)
                .start();
        //创建菜单
        RecyclerView home_menu_rc = headView.findViewById(R.id.home_menu_rv);
        GridLayoutManager gridLayoutManager = new GridLayoutManager(recyclerView.getContext(),4);
        gridLayoutManager.setOrientation(RecyclerView.VERTICAL);
        home_menu_rc.setLayoutManager(gridLayoutManager);
        HomeMenuAdapter homeMenuAdapter = new HomeMenuAdapter();
        homeMenuAdapter.setNewInstance(menuEntities);
        home_menu_rc.setAdapter(homeMenuAdapter);
        homeMenuAdapter.setOnItemChildClickListener(new OnItemChildClickListener() {
            @Override
            public void onItemChildClick(@NonNull @NotNull BaseQuickAdapter adapter, @NonNull @NotNull View view, int position) {
                ARouter.getInstance().build(MyARouter.SECKILL).navigation();
            }
        });

        //分类 recycler 布局
        RecyclerView rc = headView.findViewById(R.id.home_hot_rv);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(recyclerView.getContext());
        linearLayoutManager.setOrientation(RecyclerView.HORIZONTAL);
        rc.setLayoutManager(linearLayoutManager);
        HomeRecyclerAdapter adapter = new HomeRecyclerAdapter();
        adapter.setNewInstance(hotdata);
        rc.setAdapter(adapter);

        //画廊效果
        ViewPager viewPager = headView.findViewById(R.id.home_vp);
        viewPager.setOffscreenPageLimit(4);
        viewPager.setPageTransformer(true,pageTransformer);
        viewPager.setAdapter(new HomeTopAdapter(topEntities));
        quickAdapter.addHeaderView(headView);
        recyclerView.setAdapter(quickAdapter);
    }
    @BindingAdapter(value = "adapter")
    public static void bindAdapter(RecyclerView recyclerView, BaseQuickAdapter adapter){
        if (adapter == null)
            return;
        recyclerView.setAdapter(adapter);
    }

    @BindingAdapter(value = "orientation")
    public static void bindLinearManager(RecyclerView recyclerView,int orientation){
        LinearLayoutManager manager = new LinearLayoutManager(recyclerView.getContext());
        manager.setOrientation(orientation);
        recyclerView.setLayoutManager(manager);
    }

    @BindingAdapter(value = {"gridorientation","rvnum"})
    public static void bindGridManager(RecyclerView recyclerView,int gridorientation,int rvnum){
        GridLayoutManager manager = new GridLayoutManager(recyclerView.getContext(),rvnum);
        manager.setOrientation(gridorientation);
        recyclerView.setLayoutManager(manager);
    }
}
